package com.elm.controller;

import com.alibaba.nacos.shaded.com.google.gson.Gson;
import com.elm.entity.Orders;
import com.elm.service.OrderService;
import com.elm.service.impl.OrderServiceImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

public class OrdersController implements BaseController {
    private final OrderService orderService;
    public OrdersController() {
        orderService = new OrderServiceImpl();
    }


    @Override
    public void handleRequest(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
        // 1. 解析请求动作
        String action = req.getPathInfo().split("/")[2];

        switch (action) {
            case "createOrders":
                doCreateOrders(req, resp);
                break;
            case "listOrdersByUserId":
                doGetListOrders(req, resp);
                break;
            case "getOrdersById":
                doGetOrdersById(req, resp);
                break;
        }
    }

    /**
     * 创建订单
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     * @throws SQLException
     */
    private void doCreateOrders(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
        Orders orders = new Orders();
        orders.setUserId(req.getParameter("userId"));
        orders.setBusinessId(Integer.valueOf(req.getParameter("businessId")));
        orders.setDaId(Integer.valueOf(req.getParameter("daId")));
        orders.setOrderTotal(Double.valueOf(req.getParameter("orderTotal")));

        int orderId = orderService.doCreateOrders(orders);

        resp.setContentType("application/json;charset=UTF-8");
        resp.getWriter().write(new Gson().toJson(orderId));
    }

    /**
     * 获取订单列表
     * @param req
     * @param resp
     * @throws IOException
     * @throws SQLException
     */
    private void doGetListOrders(HttpServletRequest req, HttpServletResponse resp) throws IOException, SQLException {
        String userId = req.getParameter("userId");

        List<Orders> ordersList = orderService.doGetOrdersListByUserId(userId);

        resp.setContentType("application/json;charset=UTF-8");
        resp.getWriter().write(new Gson().toJson(ordersList));
    }

    /**
     * 根据id获取订单
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     * @throws SQLException
     */
    private void doGetOrdersById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
        String orderId = req.getParameter("orderId");

        Orders orders = orderService.getOrdersById(orderId);

        resp.setContentType("application/json;charset=UTF-8");
        resp.getWriter().write(new Gson().toJson(orders));
    }
}
